//
//  ForgetPwdViewController.m
//  Edutohome
//
//  Created by xiaofeifei on 15/1/18.
//
//

#import "ForgetPwdViewController.h"

@interface ForgetPwdViewController (){
    NSString *_code;
}

@property (weak, nonatomic) IBOutlet UITextField *accountTextfield;
@property (weak, nonatomic) IBOutlet UITextField *codeTextfield;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextfield;

@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"忘记密码";
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)getIdentifyingCode:(id)sender{//获取验证码
    if (_accountTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
        return;
    }
    [self getCode];
}
//获取验证码
- (void)getCode{
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text}];
    
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/sms",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([responseObject[@"result"] intValue] == 0) {
                                      [SVProgressHUD showInfoWithStatus:@"获取验证码失败"];
                                  }else if ([responseObject[@"result"] intValue] > 0){
                                      _code = responseObject[@"result"];
                                      [SVProgressHUD dismiss];
                                  }
                                  
                              }];
}


- (IBAction)gotoRegister:(id)sender{
    if (_accountTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号码"];
        return;
    }
    if (_codeTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        return;
    }
    if (_pwdTextfield.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    if (_pwdTextfield.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码长度不得少于6位"];
        return;
    }
    [self getRegister];
}

- (void)getRegister{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"user_name":_accountTextfield.text,@"new_pwd":_pwdTextfield.text}];
    NSLog(@"%@",parameters);
    
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@user/pwd",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([responseObject[@"result"] intValue] == 0) {
                                      [SVProgressHUD showInfoWithStatus:@"提交失败"];
                                  }else{
                                      [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                                      [self backBtnAct:nil];
                                  }
                              }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
